﻿#region Copyright & licence

// This file is part of Refix.
// 
// Refix is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
// 
// Refix is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with Refix.  If not, see <http://www.gnu.org/licenses/>.
// 
// Copyright (C) 2010-11 David Musgrove and others.

#endregion

using System;
using System.IO;
using System.Text;

using NUnit.Framework;

namespace Refix.Core.Tests
{
    [TestFixture]
    public class OutputWriterTest
    {
        [Test]
        public void WriteLine_Empty_String_Adds_Line()
        {
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            OutputWriter.Verbository = OutputVerbosity.Normal;
            OutputWriter.WriteLine();
            Assert.AreEqual(Environment.NewLine, builder.ToString(), "WriteLine didn't add line.");
        }

        [Test]
        public void Write_Empty_String_Does_Not_Add_Line()
        {
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            OutputWriter.Verbository = OutputVerbosity.Normal;
            OutputWriter.Write("");
            Assert.AreEqual(string.Empty, builder.ToString(), "Write added line.");
        }

        [Test]
        public void WriteLine_Empty_String_Obeys_Verbosity_And_Adds_Line()
        {
            OutputWriter.Verbository = OutputVerbosity.Quiet;
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            OutputWriter.WriteLine(OutputVerbosity.Normal);
            Assert.AreEqual(string.Empty, builder.ToString(), "WriteLine didn't add line.");
            OutputWriter.Verbository = OutputVerbosity.Normal;
            OutputWriter.WriteLine(OutputVerbosity.Normal);
            Assert.AreEqual(Environment.NewLine, builder.ToString(), "WriteLine didn't add line.");
        }

        [Test]
        public void WriteLine_Format_String_Obeys_Verbosity_And_Outputs_And_Adds_Line()
        {
            OutputWriter.Verbository = OutputVerbosity.Quiet;
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            const string TEST_FORMAT = "Test {0} format";
            const string TEST_PARAMETER = "Param";
            OutputWriter.WriteLine(OutputVerbosity.Normal, TEST_FORMAT, TEST_PARAMETER);
            Assert.AreEqual(string.Empty, builder.ToString(), "WriteLine didn't format, output and add line.");
            OutputWriter.Verbository = OutputVerbosity.Normal;
            OutputWriter.WriteLine(OutputVerbosity.Normal, TEST_FORMAT, TEST_PARAMETER);
            Assert.AreEqual(string.Format(TEST_FORMAT, TEST_PARAMETER) + Environment.NewLine, builder.ToString(),
                            "WriteLine didn't format, output and add line.");
        }

        [Test]
        public void Write_Format_String_Obeys_Verbosity_And_Outputs()
        {
            OutputWriter.Verbository = OutputVerbosity.Quiet;
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            const string TEST_FORMAT = "Test {0} format";
            const string TEST_PARAMETER = "Param";
            OutputWriter.WriteLine(OutputVerbosity.Normal, TEST_FORMAT, TEST_PARAMETER);
            Assert.AreEqual(string.Empty, builder.ToString(), "Write didn't format and output.");
            OutputWriter.Verbository = OutputVerbosity.Normal;
            OutputWriter.Write(OutputVerbosity.Normal, TEST_FORMAT, TEST_PARAMETER);
            Assert.AreEqual(string.Format(TEST_FORMAT, TEST_PARAMETER), builder.ToString(),
                            "Write didn't format and output.");
        }

        [Test]
        public void WriteLine_Format_String_Outputs_And_Adds_Line()
        {
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            OutputWriter.Verbository = OutputVerbosity.Normal;
            const string TEST_FORMAT = "Test {0} format";
            const string TEST_PARAMETER = "Param";
            OutputWriter.WriteLine(TEST_FORMAT, TEST_PARAMETER);
            Assert.AreEqual(string.Format(TEST_FORMAT, TEST_PARAMETER) + Environment.NewLine, builder.ToString(),
                            "WriteLine didn't format, output and add line.");
        }

        [Test]
        public void Write_Format_String_Outputs()
        {
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            OutputWriter.Verbository = OutputVerbosity.Normal;
            const string TEST_FORMAT = "Test {0} format";
            const string TEST_PARAMETER = "Param";
            OutputWriter.Write(TEST_FORMAT, TEST_PARAMETER);
            Assert.AreEqual(string.Format(TEST_FORMAT, TEST_PARAMETER), builder.ToString(),
                            "Write didn't format and output.");
        }

        [Test]
        public void WriteLine_Straight_String_Obeys_Verbosity_And_Outputs_And_Adds_Line()
        {
            OutputWriter.Verbository = OutputVerbosity.Quiet;
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            const string TEST = "Test";
            OutputWriter.WriteLine(OutputVerbosity.Normal, TEST);
            Assert.AreEqual(string.Empty, builder.ToString(), "WriteLine didn't output and add line.");
            OutputWriter.Verbository = OutputVerbosity.Normal;
            OutputWriter.WriteLine(OutputVerbosity.Normal, TEST);
            Assert.AreEqual(TEST + Environment.NewLine, builder.ToString(), "WriteLine didn't output and add line.");
        }

        [Test]
        public void Write_Straight_String_Obeys_Verbosity_And_Outputs()
        {
            OutputWriter.Verbository = OutputVerbosity.Quiet;
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            const string TEST = "Test";
            OutputWriter.WriteLine(OutputVerbosity.Normal, TEST);
            Assert.AreEqual(string.Empty, builder.ToString(), "Write didn't output.");
            OutputWriter.Verbository = OutputVerbosity.Normal;
            OutputWriter.Write(OutputVerbosity.Normal, TEST);
            Assert.AreEqual(TEST, builder.ToString(), "Write didn't output.");
        }

        [Test]
        public void WriteLine_Straight_String_Outputs_And_Adds_Line()
        {
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            OutputWriter.Verbository = OutputVerbosity.Normal;
            const string TEST = "Test";
            OutputWriter.WriteLine(TEST);
            Assert.AreEqual(TEST + Environment.NewLine, builder.ToString(), "WriteLine didn't output and add line.");
        }

        [Test]
        public void Write_Straight_String_Outputs()
        {
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            OutputWriter.Verbository = OutputVerbosity.Normal;
            const string TEST = "Test";
            OutputWriter.Write(TEST);
            Assert.AreEqual(TEST, builder.ToString(), "Write didn't output.");
        }
    }
}